package com.aaa.cars.service.impl;

import com.aaa.cars.dao.*;
import com.aaa.cars.entity.*;
import com.aaa.cars.service.CarInsuranceService;
import com.aaa.cars.service.CarService;
import com.aaa.cars.service.MaintainRecordService;
import com.aaa.common.constants.ExceptionConstant;
import com.aaa.common.constants.ReturnStatus;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (Car)表服务实现类
 *
 * @author makejava
 * @since 2020-12-04 15:54:27
 */
@Service("carService")
public class CarServiceImpl implements CarService {
    @Resource
    private CarDao carDao;

    @Resource
    private CarTypeDao carTypeDao;

    @Resource
    private BrandDao brandDao;

    @Resource
    private CarTypeConfigDetailsDao cCDDao;

    @Resource
    private StateDao stateDao;

    @Resource
    private ShopDao shopDao;

    @Resource
    private MaintainRecordService maintainRecordService;

    @Resource
    private CarInsuranceService carInsuranceService;

    @Resource
    private CarInsuranceDao carInsuranceDao;


    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Car queryById(Integer id) {
        return this.carDao.queryById(id);
    }

    @Override
    public Integer queryvin(String vin,Integer id) {
        return this.carDao.queryvin(vin,id);
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @param pageNo
     * @param pageSize
     * @param car      实例对象
     * @return 对象列表
     */
    @Override
    public Result queryAll(int pageNo, int pageSize, Car car) {
        if(pageNo==0||pageSize==0){
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        //分页
        PageHelper.startPage(pageNo,pageSize);
        List<Car> carList = carDao.queryAll(car);
        if (carList != null && carList.size() > 0) {
            for (Car car1 : carList) {
                try {
                    CarType carType = carTypeDao.queryById(car1.getType());
                    car1.setTypeName(carType.getTypeName());
                    Brand brand = brandDao.queryById(car1.getBrandId());
                    car1.setBrandName(brand.getName());
                    CarTypeConfigDetails configDetails = cCDDao.queryById(car1.getConfigId());
//                    if(configDetails==null){
//                        car1.setTypename(car1.getConfigId().toString());
////                    }else{
                        car1.setTypename(configDetails.getTypename());
//                    }
//                 State state = stateDao.queryById(car1.getState());
//                car1.setStateName(state.getStateName());
                    Shop shop = shopDao.queryById(car1.getShopId());
                    car1.setShopName(shop.getName());

                    CarInsurance carInsurance = carInsuranceDao.queryById(car1.getInsuranceId());
                    if (carInsurance != null) {
                        car1.setInsuredAmount((Double) carInsurance.getInsuredAmount());
                        car1.setThirdPartyLiability(carInsurance.getThirdPartyLiability());
                        car1.setPassengerLiability(carInsurance.getPassengerLiability());
                        car1.setPremium((Double) carInsurance.getPremium());
                        car1.setStartTime(carInsurance.getStartTime());
                        car1.setEndTime(carInsurance.getEndTime());
                        car1.setCompanyName(carInsurance.getCompanyName());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        PageInfo<Car> pageInfo = new PageInfo<Car>(carList);
        System.out.println(pageInfo);
        System.out.println(pageInfo.getNextPage());
        return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg(), pageInfo);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Car> queryAllByLimit(int offset, int limit) {
        return this.carDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param car 实例对象
     * @return 实例对象
     */
    @Override
    public Car insert(Car car) {
        car.setAddtime(new Date());
        this.carDao.insert(car);
        return car;
    }

    /**
     * 修改数据
     *
     * @param car 实例对象
     * @return 实例对象
     */
    @Override
    public Car update(Car car) {
        this.carDao.update(car);
        return this.queryById(car.getId());
    }

    @Override
    public Result maintain(Car car) {
        MaintainRecord maintainRecord = new MaintainRecord();
        maintainRecord.setCarId(car.getId());
        maintainRecord.setMaintainShopId(car.getMaintainShopId());
        maintainRecord.setRemark(car.getRemark());
        maintainRecord.setAmount(car.getAmount());
        maintainRecordService.insert(maintainRecord);
        return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg(), maintainRecord);
    }

    @Override
    public Result carInsurance(Car car) {
        CarInsurance carInsurance = new CarInsurance();
        carInsurance.setInsuredAmount(car.getInsuredAmount());
        carInsurance.setThirdPartyLiability(car.getThirdPartyLiability());
        carInsurance.setPassengerLiability(car.getPassengerLiability());
        carInsurance.setNumber(car.getId());
        carInsurance.setPremium(car.getPremium());
        carInsurance.setStartTime(car.getStartTime());
        carInsurance.setEndTime(car.getEndTime());
        carInsurance.setCompanyName(car.getCompanyName());
        carInsuranceService.insert(carInsurance);
        return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg(), carInsurance);
    }

    @Override
    public Result deleteInsurance(Car car) {
        car.setInsuranceId(0);
        carDao.update(car);
        return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg());
    }

    @Override
    public Result queryAllCar(int pageNo, int pageSize, Car car) {
        if(pageNo==0||pageSize==0){
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        //分页
        PageHelper.startPage(pageNo,pageSize);
        car.setState(1);
        List<Car> carList = carDao.queryAll(car);
        if (carList != null && carList.size() > 0) {
            for (Car car1 : carList) {
                CarType carType = carTypeDao.queryById(car1.getType());
                car1.setTypeName(carType.getTypeName());
                Brand brand = brandDao.queryById(car1.getBrandId());
                car1.setBrandName(brand.getName());
                CarTypeConfigDetails configDetails = cCDDao.queryById(car1.getConfigId());
                car1.setTypename(configDetails.getTypename());
                State state = stateDao.queryById(car1.getState());
                car1.setStateName(state.getStateName());
                Shop shop = shopDao.queryById(car1.getShopId());
                car1.setShopName(shop.getName());
                CarInsurance carInsurance = carInsuranceDao.queryById(car1.getInsuranceId());
                if (carInsurance != null) {
                    car1.setInsuredAmount((Double) carInsurance.getInsuredAmount());
                    car1.setThirdPartyLiability(carInsurance.getThirdPartyLiability());
                    car1.setPassengerLiability(carInsurance.getPassengerLiability());
                    car1.setPremium((Double) carInsurance.getPremium());
                    car1.setStartTime(carInsurance.getStartTime());
                    car1.setEndTime(carInsurance.getEndTime());
                    car1.setCompanyName(carInsurance.getCompanyName());
                }
            }
        }
        PageInfo<Car> pageInfo = new PageInfo<Car>(carList);
        return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg(), pageInfo);
    }



}